package com.ruoyi.sale.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.CodeGenerateUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.sale.domain.*;
import com.ruoyi.sale.mapper.*;
import com.ruoyi.sale.vo.SaleOrderVo;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.service.ISaleOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@Service
public class SaleOrderServiceImpl implements ISaleOrderService 
{
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private SysCustomerMapper customerMapper;
    @Autowired
    private SaleOrderDetailMapper saleOrderDetailMapper;
    @Autowired
    private SysAccountMapper accountMapper;
    @Autowired
    private SalePayorderMapper salePayorderMapper;


    /**
     * 查询销售订单
     * 
     * @param id 销售订单主键
     * @return 销售订单
     */
    @Override
    public SaleOrder selectSaleOrderById(Long id)
    {
        return saleOrderMapper.selectSaleOrderById(id);
    }

    /**
     * 查询销售订单列表
     * 
     * @param saleOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<SaleOrder> selectSaleOrderList(SaleOrder saleOrder)
    {
        return saleOrderMapper.selectSaleOrderList(saleOrder);
    }

    /**
     * @description: 根据客户id查询已经通过审核的销售单
     * @param: id
     * @return: java.util.List<com.ruoyi.sale.domain.SaleOrder>
     * @author clqlongbao
     * @date: 2022/9/2 19:55
     */
    @Override
    public List<SaleOrder> selectCheckedOrderById(Long id) {
        return saleOrderMapper.selectCheckedOrderById(id);
    }

    /**
     * 新增销售订单
     * 
     * @param
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSaleOrder(SaleOrderVo saleOrderVo){
        int row = 0;
        //获取当前登陆人
        LoginUser user = SecurityUtils.getLoginUser();
        System.out.println(user.toString());
        //生成订单号
        String orderNo = CodeGenerateUtils.generateOrderSn(user.getUserid());
        System.out.println("销售订单号为："+orderNo);
        String productName = saleOrderVo.getSelectGoodsList().stream().map(SysProduct::getName).collect(Collectors.toList()).toString();
        //保存销售主订单
        SaleOrder saleOrder = new SaleOrder();
        BeanUtils.copyProperties(saleOrderVo,saleOrder);
        saleOrder.setAccount(saleOrderVo.getAccountId().toString());
        saleOrder.setOrderNo(Long.valueOf(orderNo));
        saleOrder.setStatus(saleOrderVo.getOrderStatus());
        saleOrder.setCustomerName(customerMapper.selectSysCustomerById(saleOrderVo.getCustomerId()).getName());
        saleOrder.setProductName(productName);
        saleOrder.setCreateBy(user.getUsername());
        saleOrder.setCreateTime(new Date());
        row = saleOrderMapper.insertSaleOrder(saleOrder);
        System.out.println("保存主订单成功");
        //保存销售明细订单
        saleOrderVo.getSelectGoodsList().forEach(product -> {
            SaleOrderDetail saleOrderDetail = new SaleOrderDetail();
            BeanUtils.copyProperties(product,saleOrderDetail);
            saleOrderDetail.setId(null);
            saleOrderDetail.setProductName(product.getName());
            saleOrderDetail.setOrderNo(saleOrder.getOrderNo());
            saleOrderDetail.setPrice(product.getSalePrice());
            saleOrderDetail.setAmount(product.getSalePrice()*product.getNumber());
            saleOrderDetailMapper.insertSaleOrderDetail(saleOrderDetail);
            System.out.println("销售明细单保存成功");
        });
        //如果支付了定金，需要生成支付单
        if(saleOrderVo.getEarnest()!=0){
            //使用的账户
            SysAccount account = accountMapper.selectSysAccountById(saleOrderVo.getAccountId());
            //封装支付单
            SalePayorder salePayorder = new SalePayorder();
            salePayorder.setOrderNo(Long.valueOf(orderNo));
            salePayorder.setAccountId(saleOrderVo.getAccountId());
            salePayorder.setProductName(productName);
            salePayorder.setAccountName(account.getName());
            salePayorder.setAmount(saleOrderVo.getAmount());
            salePayorder.setEarnest(saleOrderVo.getEarnest());
            salePayorder.setPay(saleOrderVo.getPay());
            salePayorder.setReduced(saleOrderVo.getReduced());
            salePayorder.setUserId(user.getUserid());
            salePayorder.setCreateTime(new Date());
            row = salePayorderMapper.insertSalePayorder(salePayorder);
            System.out.println("支付单保存成功");
            //对应账户需要增加定金的钱
            account.setBalance(account.getBalance()+saleOrderVo.getEarnest());
            accountMapper.updateSysAccount(account);
            System.out.println("账户增加定金成功");
        }
        return row;
    }

    /**
     * 修改销售订单
     * 
     * @param saleOrder 销售订单
     * @return 结果
     */
    @Override
    public int updateSaleOrder(SaleOrder saleOrder)
    {
        saleOrder.setUpdateTime(DateUtils.getNowDate());
        return saleOrderMapper.updateSaleOrder(saleOrder);
    }

    /**
     * 批量删除销售订单
     * 
     * @param ids 需要删除的销售订单主键
     * @return 结果
     */
    @Override
    public int deleteSaleOrderByIds(Long[] ids)
    {
        return saleOrderMapper.deleteSaleOrderByIds(ids);
    }

    /**
     * 删除销售订单信息
     * 
     * @param id 销售订单主键
     * @return 结果
     */
    @Override
    public int deleteSaleOrderById(Long id)
    {
        return saleOrderMapper.deleteSaleOrderById(id);
    }
}
